"""
Case Type   : fullsql
Case Name   : 校验log_min_duration_statement为-1(默认参数不会记录slowsql)
Create At   : 2022/02/08
Owner       : zhao-pengcheng-g
Description :
    1.设置track_stmt_stat_level为OFF,L0，
    设置log_min_duration_statement为-1，
    创建表,插入数据
    2.查询系统视图
    3.设置track_stmt_stat_level='l1,l1'，
    设置log_min_duration_statement为-1，插入数据
    4.查询系统视图
    5.环境清理
Expect      :
    1.成功
    2.数据是空的，(0 row)
    3.成功
    4.(1 row)
    5.成功
History     :
"""

import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Fullsql(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        text = '---Opengauss_Function_Fullsql_Case0034 start---'
        self.logger.info(text)
        self.userNode = Node('PrimaryDbUser')
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.table_name = 't_fullsql_0034'

    def test_fullsql(self):
        text = '---step1:设置track_stmt_stat_level为OFF,L0，' \
               '设置log_min_duration_statement为-1，' \
               '创建表,插入数据,expect:成功---'
        self.logger.info(text)
        create_cmd = f'''set track_stmt_stat_level='OFF,L0';
            show track_stmt_stat_level;
            set log_min_duration_statement= -1;
            show log_min_duration_statement;
            drop table if exists {self.table_name};
            create table {self.table_name}(a int, b int);
            insert into {self.table_name} values (1,2);
            select pg_sleep(30);'''
        create_msg = self.sh_primy.execut_db_sql(create_cmd)
        self.logger.info(create_msg)
        self.assertIn('OFF,L0', create_msg.splitlines()[3], '执行失败:' + text)
        self.assertIn('-1', create_msg.splitlines()[9], '执行失败:' + text)
        self.assertTrue(create_msg.count(self.constant.SET_SUCCESS_MSG) == 2,
                        '执行失败:' + text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS and
                      self.constant.INSERT_SUCCESS_MSG,
                      create_msg, '执行失败:' + text)

        text = '---step2:查询系统视图,expect:数据是空的，(0 rows)---'
        self.logger.info(text)
        select_cmd = f'''select query, is_slow_sql \
            from statement_history where query like '%{self.table_name}%';'''
        select_msg = self.sh_primy.execut_db_sql(select_cmd, dbname='postgres')
        self.logger.info(select_msg)
        self.assertIn('(0 rows)', select_msg, '执行失败:' + text)

        text = '---step3:设置track_stmt_stat_level为l1,l1，' \
               '设置log_min_duration_statement为-1，插入数据,' \
               'expect:成功---'
        self.logger.info(text)
        insert_cmd = f'''set track_stmt_stat_level='l1,l1';
            show track_stmt_stat_level;
            set log_min_duration_statement= -1;
            show log_min_duration_statement;
            insert into {self.table_name} values(1,2);
            select pg_sleep(30);'''
        insert_msg = self.sh_primy.execut_db_sql(insert_cmd)
        self.logger.info(insert_msg)
        self.assertTrue(insert_msg.count(self.constant.SET_SUCCESS_MSG) == 2,
                        '执行失败:' + text)
        self.assertIn('l1,l1', insert_msg.splitlines()[3], '执行失败:' + text)
        self.assertIn('-1', insert_msg.splitlines()[9], '执行失败:' + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG,
                      insert_msg, '执行失败:' + text)

        text = '---step4:查询系统视图,expect:(1 row)---'
        self.logger.info(text)
        select_cmd = f'''select query, is_slow_sql from statement_history \
                         where query like '%{self.table_name}%';'''
        select_msg = self.sh_primy.execut_db_sql(select_cmd, dbname='postgres')
        self.logger.info(select_msg)
        self.assertIn('(1 row)', select_msg, '执行失败:' + text)

    def tearDown(self):
        text = '---step5: 环境清理,expect:成功:---'
        self.logger.info(text)
        delete_cmd = f'''delete from statement_history \
            where query like '%{self.table_name}%';'''
        delete_msg = self.sh_primy.execut_db_sql(delete_cmd, dbname='postgres')
        self.logger.info(delete_msg)
        drop_cmd = f'drop table {self.table_name};'
        drop_msg = self.sh_primy.execut_db_sql(drop_cmd)
        self.logger.info(drop_msg)
        self.assertIn(self.constant.DELETE_SUCCESS_MSG,
                      delete_msg, '执行失败:' + text)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS,
                      drop_msg, '执行失败:' + text)
        self.logger.info(
            '---Opengauss_Function_Fullsql_Case0034 finish---')
